﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="productadd.aspx.cs" Inherits="UI.system.page.product.productadd" %>
<%@ Import Namespace="Model" %>
<%@ Import Namespace="Common" %>
<style>
    .line{width:100%; float:left; min-height:48px; line-height:38px;}
    .lines{width:100%; float:left; min-height:48px; line-height:38px;}
    .line input{margin-bottom:10px;}
    .delete{color:#fa3636;}
    .delete:hover{color:#be0404;}
</style>
<div style="margin: 15px;">
	<form class="layui-form" style="padding-left:0px;">
        <div class="layui-form-item">
			<label class="layui-form-label"><font>商品分类</font><em>*</em></label>
			<div class="layui-input-block">
                <div style="float:left; margin-right:10px;">
				<select id="category1id" name="category1id" lay-verify="required" lay-filter="category1id" style="width:200px;">
				</select>
                </div>
                <div style="float:left;">
                <select id="category2id" name="category2id" lay-verify="required" lay-filter="category2id" style="width:200px;">
				</select>
                </div>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>商品编号</font><em>*</em></label>
			<div class="layui-input-block">
                <input type="text" name="num" placeholder="请输入商品编号" lay-verify="required" autocomplete="off" class="layui-input" value="<%=info.Num %>">
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>商品名称</font><em>*</em></label>
			<div class="layui-input-block">
                <input type="text" name="name" placeholder="请输入商品名称" lay-verify="required" autocomplete="off" class="layui-input" value="<%=info.Name %>">
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>商品图片</font><em>*</em></label>
			<div class="layui-input-block" lay-verify="img">
                <div id="img_panel" class="imgview">
                    <%if(info.Imgs.Length>0){
                      foreach(string img in info.Imgs){ 
                    %>
                        <div><img class="img" src="<%=img %>"/><div id="<%=img %>" class="delete" onclick="RemoveFile(this)"><img src="/system/img/delete.png"></div></div>
                    <%}} %>
                </div>
				<div id="upload_file_o"></div>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>单/多价格</font><em>*</em></label>
			<div class="layui-input-block">
				<input type="radio" name="pricetype" lay-filter="pricetype" lay-verify="pricetype" value="1" title="单个价格" <%=info.PriceType==1?"checked='checked'":"" %>>
                <input type="radio" name="pricetype" lay-filter="pricetype" lay-verify="pricetype" value="2" title="多个价格" <%=info.PriceType==2?"checked='checked'":"" %>>
			</div>
		</div>
        <label id="pricetype1" class='<%=info.PriceType==1?"":"hidden" %>'>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>单位/规格</font><em>*</em></label>
			<div class="layui-input-block">
				<input id="unit" type="text" name="unit" placeholder="请输入商品单位或规格，例如个、箱、台、件、块、张" autocomplete="off" class="layui-input" value='<%=info.Unit %>'>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>销售价</font><em>*</em></label>
			<div class="layui-input-block">
				<input id="aprice" type="text" name="aprice" placeholder="请输入优惠价" autocomplete="off" class="layui-input" value='<%=info.APrice %>'>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>市场价</font></label>
			<div class="layui-input-block">
				<input id="oprice" type="text" name="oprice" placeholder="请输入市场价" autocomplete="off" class="layui-input" value='<%=info.OPrice %>'>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>商品销量</font><em>*</em></label>
			<div class="layui-input-block">
				<input id="sellcount" type="text" name="sellcount" placeholder="请输入商品销量" autocomplete="off" class="layui-input" value='<%=info.SellCount %>'>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>商品库存</font><em>*</em></label>
			<div class="layui-input-block">
				<input id="librarycount" type="text" name="librarycount" placeholder="请输入商品库存" autocomplete="off" class="layui-input" value='<%=info.LibraryCount %>'>
			</div>
		</div>
        </label>
        <label id="pricetype2" class='<%=info.PriceType==1?"hidden":"" %>'>
        <div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-inline" style="width:660px; height:auto;">
                <div class="lines">
				    <input type="text" name="pricename" placeholder="规格名称，例如：选择颜色" autocomplete="off" class="layui-input" value='<%=info.PriceName %>' style="width:240px; float:left; margin-right:10px; ">
                </div>
                <%if(id==0){ %>
                <div class="line">
				    <input type="text" placeholder="单位/规格，例：白色、32G、个" autocomplete="off" class="layui-input unit" value='' style="width:240px; float:left; margin-right:10px; ">
                    <input type="text" placeholder="市场价" autocomplete="off" class="layui-input oprice" value='' style="width:100px; float:left; margin-right:10px;">
                    <input type="text" placeholder="优惠价" autocomplete="off" class="layui-input aprice" value='' style="width:100px; float:left; margin-right:10px;">
                    <input type="text" placeholder="商品库存" autocomplete="off" class="layui-input librarycount" value='' style="width:150px; float:left; margin-right:10px;">
                </div>
                <div class="line">
                    <input type="text" placeholder="单位/规格，例：黑色、64G、箱" autocomplete="off" class="layui-input unit" value='' style="width:240px; float:left; margin-right:10px;">
                    <input type="text" placeholder="市场价" autocomplete="off" class="layui-input oprice" value='' style="width:100px; float:left; margin-right:10px;">
                    <input type="text" placeholder="优惠价" autocomplete="off" class="layui-input aprice" value='' style="width:100px; float:left; margin-right:10px;">
                    <input type="text" placeholder="商品库存" autocomplete="off" class="layui-input librarycount" value='' style="width:150px; float:left; margin-right:10px;">
                </div>
                <%}else{
                      int count = 0;
                      foreach(PriceInfo price in pricelist){
                          count++;
                %>
                <div class="line">
				    <input type="text" placeholder="单位/规格，例：白色、32G、个" autocomplete="off" class="layui-input unit" value='<%=price.Unit %>' style="width:240px; float:left; margin-right:10px; ">
                    <input type="text" placeholder="市场价" autocomplete="off" class="layui-input oprice" value='<%=price.OPrice %>' style="width:100px; float:left; margin-right:10px;">
                    <input type="text" placeholder="优惠价" autocomplete="off" class="layui-input aprice" value='<%=price.APrice %>' style="width:100px; float:left; margin-right:10px;">
                    <input type="text" placeholder="商品库存" autocomplete="off" class="layui-input librarycount" value='<%=price.LibraryCount %>' style="width:150px; float:left; margin-right:10px;"><%if(count>2){ %><a class="delete" onclick="DeletePrice(this)">删除</a><%} %>
                </div>
                <%}} %>
                <div class="lines">
                    <a onclick="AddPrice(this)">新增价格</a>
                </div>
			</div>

		</div>
        </label>
        <label id="format">
        <div class="layui-form-item">
			<label class="layui-form-label"><font>更多规格</font></label>
			<div class="layui-input-inline" style="width:660px; height:auto;">
                <%  foreach(string fromat in formats){
                    string[] ps = fromat.Split('&');
                %>
                <div class="line">
		            <input type="text" placeholder="规格名称，例如：尺寸" autocomplete="off" class="layui-input names" value="<%=ps[0] %>" style="width:170px; float:left; margin-right:10px; ">
                    <%for(int i=1;i<ps.Length;i++){ %>
                    <input type="text" placeholder="规格值" autocomplete="off" class="layui-input format" value="<%=ps[i] %>" style="width:80px; float:left; margin-right:10px;">
                    <%} %>
                    <div style="float:left; line-height:20px;">
                        <a onclick="AddParaOption(this)">新增</a><br /><a class="delete" onclick="DeleteParaOption(this)">删除</a>
                    </div>
                </div>
                <%} %>
                <div class="lines">
				    <a onclick="AddPara(this)">新增规格</a>
			    </div>
			</div>
		</div>
        </label>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>商品参数</font></label>
			<div class="layui-input-inline" style="width:660px; height:auto;">
                <div class="lines">
				    <a id="setparam" onclick="ShowParam(this)" setparam="0">参数设置</a>
			    </div>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>快递费用</font><em>*</em></label>
			<div class="layui-input-block">
				<input id="expressmoney" type="text" name="expressmoney" placeholder="请输入快递费用" autocomplete="off" class="layui-input" value='<%=info.ExpressMoney %>'>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>产品标签</font><em>*</em></label>
			<div class="layui-input-block tags" style="padding-top:10px;">
                <%foreach(TagsInfo tags in tagslist){ %>
				<input type="checkbox" name="btns" lay-skin="primary" title="<%=tags.Name %>" value="<%=tags.ID %>" <%=info.Tags.Split(',').Contains(tags.ID.ToString())?"checked":"" %>>
                <%} %>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>显示顺序</font><em>*</em></label>
			<div class="layui-input-block">
				<input type="text" name="sort" placeholder="请输入显示顺序" lay-verify="required" autocomplete="off" class="layui-input" value='<%=info.Sort %>'>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>上架/下架</font><em>*</em></label>
			<div class="layui-input-block">
				<input type="radio" name="updown" lay-filter="updown" lay-verify="updown" value="1" title="上架" <%=info.UpDown==1?"checked='checked'":"" %>>
                <input type="radio" name="updown" lay-filter="updown" lay-verify="updown" value="0" title="下架" <%=info.UpDown==0?"checked='checked'":"" %>>
			</div>
		</div>
        <div class="layui-form-item">
			<label class="layui-form-label"><font>商品详情</font><em>*</em></label>
			<div class="layui-input-block" lay-verify="context">
                <script id="context" type="text/plain" style="width:100%;height:250px;"><%=info.Context %></script>
			</div>
		</div>
		<a lay-filter="save" lay-submit style="display: none;"></a>
	</form>
</div>
<script>
    layui.use(["form", "table", "laydate"], function () {
        var $ = layui.jquery, form = layui.form, laydate = layui.laydate;
        table = layui.table;
        form.on("radio(pricetype)",function(data){
            if(data.value=="1"){
                $("#pricetype1").show();
                $("#pricetype2").hide();
            }else{
                $("#pricetype1").hide();
                $("#pricetype2").show();
            }
        });
    });
    var categorys = eval('<%=categorys %>');
    var category1id=<%=info.Category1ID%>;
    var category2id=<%=info.Category2ID%>;
    
    function Init_Category1() {
        var buffer=new StringBuffer();
        $.each(categorys,function(i,info){
            if(info.ParentID==0){
                buffer.append('<option value="'+info.ID+'" '+ (info.ID==category1id?"selected":"")+'>'+info.Name+'</option>');
            }
        });
        if(categorys.length==0){
            $("#category1id").html('<option value="0">暂无分类</option>');
        }else{
            $("#category1id").html(buffer.toString());
        }
        form.render("select");
    };
    function Init_Category2(parentid) {
        var buffer=new StringBuffer();
        $.each(categorys,function(i,info){
            if(info.ParentID==parentid){
                buffer.append('<option value="'+info.ID+'" '+ (info.ID==category2id?"selected":"")+'>'+info.Name+'</option>');
            }
        });
        if(buffer.toString()==""){
            $("#category2id").html('<option value="0">暂无分类</option>').parent().hide();
        }else{
            $("#category2id").html(buffer.toString()).parent().show();
        }
        form.render("select");
    };
    form.on("select(category1id)",function(){
        Init_Category2($("#category1id").val());
        form.render("category1id");
    });
    Init_Category1();
    Init_Category2($("#category1id").val());


    function AddPrice(o){
        var str='';
        str+='<div class="line">';
		     str+='<input type="text" placeholder="单位/规格，例：白色、32G、个" autocomplete="off" class="layui-input unit" value="" style="width:240px; float:left; margin-right:10px; ">';
             str+='<input type="text" placeholder="市场价" autocomplete="off" class="layui-input oprice" value="" style="width:100px; float:left; margin-right:10px;">';
             str+='<input type="text" placeholder="优惠价" autocomplete="off" class="layui-input aprice" value="" style="width:100px; float:left; margin-right:10px;">';
             str+='<input type="text" placeholder="商品库存" autocomplete="off" class="layui-input librarycount" value="" style="width:150px; float:left; margin-right:10px;"><a class="delete" onclick="DeletePrice(this)">删除</a>';
        str+='</div>';
        $(str).insertBefore($(o).parent());
    };
    function DeletePrice(o){
        $(o).parent().remove();
    };
    function AddPara(o){
        if($("#para .line").length>=6)
        {
            LayerBox.Error("最多可以添加5个参数");
            return;
        }
        var str='';      
        str+='<div class="line">';
		    str+='<input type="text" placeholder="规格名称，例如：尺寸" autocomplete="off" class="layui-input names" value="" style="width:170px; float:left; margin-right:10px; ">';
            str+='<input type="text" placeholder="M号" autocomplete="off" class="layui-input format" value="" style="width:80px; float:left; margin-right:10px;">';
            str+='<input type="text" placeholder="L号" autocomplete="off" class="layui-input format" value="" style="width:80px; float:left; margin-right:10px;">';
            str+='<input type="text" placeholder="XL号" autocomplete="off" class="layui-input format" value="" style="width:80px; float:left; margin-right:10px;">';
            str+='<div style="float:left; line-height:20px;">';
            str+='<a onclick="AddParaOption(this)">新增</a><br /><a class="delete" onclick="DeleteParaOption(this)">删除</a>';
            str+='</div>';
        str+='</div>';
        $(str).insertBefore($(o).parent());
    };
    function AddParaOption(o){
        $('<input type="text" placeholder="规格值" autocomplete="off" class="layui-input format" value="" style="width:80px; float:left; margin-right:10px;">').insertBefore($(o).parent());
    };
    function DeleteParaOption(o){
        
        var index=layer.confirm('请选择您要删除的选项', {
          area: ['380px', '160px'],
          btn: ['删除整行参数', '删除最后一项参数', '取消'], //可以无限个按钮
              btn3: function(index, layero){
                layer.close(index);
              }
          }, 
          function(index, layero){
            $(o).parent().parent().remove();
            layer.close(index);
          }, 
          function(index){
            if($(o).parent().parent().find("input").length==3){
                LayerBox.Error("至少保留2项");
                return;
            }
            $(o).parent().parent().find("input:last").remove();
            layer.close(index);
          });
        
    };
    function ShowParam() {
        Ajax.Get("/system/page/product/productparam.aspx?productid=<%=id %>&setparam="+$("#setparam").attr("setparam")+"&category1id="+$("#category1id").val()+"&category2id="+$("#category2id").val(), function (html) {
            product_index = LayerBox.Search("参数设置", html, "700px", "85%");
        });
    };
</script>